$(document).ready(function(){
    
    // Handle clicking on the check mark which marks the message as important
    $(".check").each(function(){
        // The title attribute holds the url information
        var myUrl = $(this).attr("title");
        $(this).click(function(){
            var object = $(this);
            $(this).toggleClass("active");
            // Make the call to the database
            $.ajax({
                type:"GET",
                url:myUrl,
                error: onError
            });
        });
    });
    // Handle the drowpdown to check or uncheck multiple items
    $("#checkuncheck>option").mousedown(function(){
        var option = $("select option:selected").val();
        if(option == "All")
        {
            $("input[type=checkbox]").attr('checked',true);
        }
        else if(option == "None")
        {
            $("input[type=checkbox]").attr('checked',false);
        }
    });

    // Handle updating the message as read
    $("#markread").click(function(){
        var object = $(this);
        $("input:checked").each(function(){
            // Make it look like its read
            if(!$(this).parent().hasClass("read"))
            {
                $(this).parent().addClass("read");
            }
            // Make the call to save it in the database
            var messageID = $(this).attr('name');
            $.ajax({
                type:"GET",
                url:"/modifyread/1/"+messageID,
                error: onError
            });
        });
    });

    // Handle updating the message as unread
    $("#markunread").click(function(){
        var object = $(this);
        $("input:checked").each(function(){
            // Make it look unread
            if($(this).parent().hasClass("read"))
            {
                $(this).parent().removeClass("read");
            }
            // Make the call to save it in the database
            var messageID = $(this).attr('name');
            $.ajax({
                type:"GET",
                url:"/modifyread/0/"+messageID,
                error: onError
            });
        });
    });

    
    function onError()
    {
        alert("error");
    }

});
